//
//  HMMainViewController.swift
//  Weibo
//
//  Created by 米易雅克 on 16/4/8.
//  Copyright © 2016年 米易雅克. All rights reserved.
//

import UIKit
import SVProgressHUD
class HMMainViewController: UITabBarController {

    override func viewDidLoad() {
        super.viewDidLoad()

        let mainTabBar = HMMainTabBar()
        
        mainTabBar.composeClosure = {
        
        print("加号被点击了")
            
            if !HMUserAccountViewModel.sharedInstance.userLogin {
                SVProgressHUD.showErrorWithStatus("请先登录 ...")
                return
            }
            //创建转发微博界面
            let composeVc = HMComposeViewController()
            
            let  nav = UINavigationController(rootViewController: composeVc)
            
            self.presentViewController(nav, animated: true, completion: nil)
            
            
        
        }
        setValue(mainTabBar, forKey: "tabBar")
        setupChildViewControllers()
    }

    private func setupChildViewControllers(){
    
    
        //      首页
        let homeVc = HMHomeTableViewController()
//        homeVc.title = "首页"
//        
//        homeVc.tabBarItem.image = UIImage(named: "tabbar_home")
//        homeVc.tabBarItem.selectedImage = UIImage(named: "tabbar_home_selected")
//        addChildViewController(UINavigationController(rootViewController : homeVc))
        
        setupChildViewController(homeVc, imageName: "tabbar_home", title: "首页")
        //  消息
        let message = HMMessageTableViewController()
//        message.title = "信息"
//        message.tabBarItem.image = UIImage(named: "tabbar_message_center")
//        self.addChildViewController(UINavigationController (rootViewController: message))
        setupChildViewController(message, imageName: "tabbar_message_center", title: "信息")

        
        //  发现
        let discover = HMDiscoverTableViewController()
//        discover.title = "发现"
//        discover.tabBarItem.image = UIImage(named: "tabbar_discover")
//        self .addChildViewController(UINavigationController.init(rootViewController: discover))
setupChildViewController(discover, imageName: "tabbar_discover", title: "发现")
        
        
        //  我
        let profile = HMProfileTableViewController()
//        profile.title = "我"
//        profile.tabBarItem.image = UIImage(named: "tabbar_profile")
//        self.addChildViewController(UINavigationController.init(rootViewController: profile))

        setupChildViewController(profile, imageName: "tabbar_profile", title: "我")

    
    }
    
    
    private func setupChildViewController(controller : UIViewController, imageName : String, title : String){
        
        controller.title = title
        controller.tabBarItem.image = UIImage(named: imageName)
        
        controller.tabBarItem.selectedImage = UIImage(named: imageName + "_selected")?.imageWithRenderingMode(UIImageRenderingMode.AlwaysOriginal)
        
        controller.tabBarItem.setTitleTextAttributes([NSForegroundColorAttributeName : UIColor.orangeColor()], forState: UIControlState.Selected)
        
        addChildViewController(UINavigationController.init(rootViewController: controller))
      
        
    }
   
}
